package com.xiangwushuo.framework.activity.web

import androidx.databinding.DataBindingUtil
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.lib.common.base.mvvm.CommonViewModelActivity
import com.lib.common.constant.INTENT_KEY_ACTION
import com.lib.common.constant.INTENT_KEY_TITILE
import com.lib.common.constant.INTENT_KEY_URL
import com.lib.common.constant.RouterPathConstant
import com.lib.framework.entity.Event
import com.lib.framework.manager.LogManager

import com.xiangwushuo.framework.R
import com.xiangwushuo.framework.activity.web.fragment.WebFragment
import com.xiangwushuo.framework.databinding.ActivityWebBinding

/**
 * Copyright (C), 2019-2020
 * FileName: WebActivity
 * Author:
 * Date: 2020-01-01 00:00
 * Description: XXX 页面
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
@Route(path = RouterPathConstant.PATH_WEB_VIEW)
class WebActivity : CommonViewModelActivity<WebViewModel>() {

    companion object {

        /**
         * 发给 activity 的事件
         */
        const val EVENT_XXX = 1 // XXX 事件

        const val ACTION_NONE = "1" // 默认操作

        /**
         * 获取启动该页面需要的 Bundle
         */
        fun getStartActivityBundle(title: String, url: String, action: String = ACTION_NONE): Bundle {
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TITILE, title)
            bundle.putString(INTENT_KEY_URL, url)
            bundle.putString(INTENT_KEY_ACTION, action)

            return bundle
        }
    }

    private var mDataBinding: ActivityWebBinding? = null
    private var mWebFragment: WebFragment? = null

    override fun getViewModelClass() = WebViewModel::class.java

    override fun onCreate(savedInstanceState: Bundle?) {
        mIsSetDefaultStatusBar = false
        super.onCreate(savedInstanceState)
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_web)
        mDataBinding?.viewModel = mViewModel

        initView()
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .init()
        mDataBinding?.ivBack?.setOnClickListener {
            onBackPressed()
        }

        mDataBinding?.tvTitle?.text = mViewModel.mTitle

        val webFragment = WebFragment.newInstance(mViewModel.mTitle, mViewModel.mUrl)
        mWebFragment = webFragment

        supportFragmentManager.beginTransaction().add(R.id.container, webFragment, "WebFragment")
            .commit()
    }

    override fun dealEvent(event: Event?): Boolean {
        // 处理 viewModel 发送过来的事件
        when (event?.code) {
            EVENT_XXX -> {
                val extraObj = event.extraObj
                if (extraObj is String) {
                    LogManager.i(mTag, "XXX 事件 的额外参数 extraObj = $extraObj")
                }
                return true
            }
        }

        return super.dealEvent(event)
    }

    override fun onBackPressed() {
        if (mWebFragment?.onBackPressed() == true) {
            return
        }

        super.onBackPressed()
    }
}
